package leecode.数组和字符串.移动零;

import java.util.Arrays;

public class moveZeroes {
    public static void main(String[] args) {
        int[] nums = new int[]{0};
        Soulution soulution = new Soulution();
        soulution.moveZeroes(nums);
        System.out.println(Arrays.toString(nums));
    }

    static class Soulution{
        //我的方法 太冗余了
        public void moveZeroes(int[] nums){
            int slow = 0,fast = 1;
            int temp ;
            if (nums.length==1){
            }else {
                for (int i = 0; i < nums.length; i++) {
                    if (nums[slow]!=0 ){
                        slow++;

                    }else if (nums[slow]==0 && nums[fast] !=0){
                        temp = nums[fast];
                        nums[fast] = nums[slow];
                        nums[slow] = temp;
                        slow++;
                    }
                    if (fast<nums.length-1){
                        fast++;
                    }

                }
            }


        }

        //力扣官方答案
        public void moveZeroes2(int[] nums) {
            int n = nums.length, left = 0, right = 0;
            while (right < n) {
                if (nums[right] != 0) {
                    swap(nums, left, right);
                    left++;
                }
                right++;
            }
        }

        public void swap(int[] nums, int left, int right) {
            int temp = nums[left];
            nums[left] = nums[right];
            nums[right] = temp;
        }




    }
}
